package com.foreveross.bsl.mam.app.application.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.query.Query;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.web.QueryFilter;
import com.foreveross.bsl.common.utils.web.QueryFilter.Operator;
import com.foreveross.bsl.common.utils.web.QuerySpecs;
import com.foreveross.bsl.mam.app.application.AppVersionApplication;
import com.foreveross.bsl.mam.app.application.vo.AppVersionVo;
import com.foreveross.bsl.mam.model.app.domain.AppVersion;
import com.foreveross.bsl.mongodb.repository.DynamicQueryBuilder;

/**
 * @author tanzhixiong
 * @2013-6-22 @下午2:04:36
 */
@Named("appVersionApplication")
public class AppVersionApplicationImpl implements AppVersionApplication {

	@Override
	public AppVersionVo saveEntity(AppVersionVo entity) {
		AppVersion appVersion = new AppVersion();
		BeanHelper.copyProperties(appVersion, entity);
		appVersion = (AppVersion) appVersion.save();
		entity.setId(appVersion.getId());
		return entity;
	}

	@Override
	public AppVersionVo updateEntity(String id, AppVersionVo entity) {
		AppVersion appVersion = new AppVersion();
		appVersion = (AppVersion) appVersion.findOne(id);
		BeanHelper.copyProperties(appVersion, entity);
		appVersion.save();
		BeanHelper.copyProperties(entity, appVersion);
		return entity;
	}

	@Override
	public void deleteEntity(String id) {
		AppVersion appVersion = new AppVersion();
		appVersion = (AppVersion) appVersion.findOne(id);
		appVersion.delete();

	}

	@Override
	public AppVersionVo getEntity(String id) {
		AppVersion appVersion = new AppVersion();
		appVersion = (AppVersion) appVersion.findOne(id);
		if (appVersion != null) {
			AppVersionVo appVersionVo = new AppVersionVo();
			BeanHelper.copyProperties(appVersionVo, appVersion);
			return appVersionVo;
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<AppVersionVo> findAll() {
		AppVersion appVersion = new AppVersion();
		Iterable iterable = appVersion.findAll();
		if (iterable != null) {
			List<AppVersionVo> result = new ArrayList<AppVersionVo>();
			AppVersionVo vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				AppVersion obj = (AppVersion) iterator.next();
				vo = new AppVersionVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	@Override
	public Page<AppVersionVo> findByPageable(Pageable pageable) {
		AppVersion appVersion = new AppVersion();
		Page page = appVersion.findAll(pageable);
		if (page != null && page.getContent() != null) {
			List<AppVersionVo> result = new ArrayList<AppVersionVo>();
			AppVersionVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				AppVersion obj = (AppVersion) iterator.next();
				vo = new AppVersionVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<AppVersionVo> pageVO = new PageImpl<AppVersionVo>(result,
					pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppVersionVo> findByPlatform(String platfrom, String appId) {
		// 添加查询条件
		List<QueryFilter> filters = new ArrayList<QueryFilter>();
		QueryFilter appidfilter = new QueryFilter("applicationid", Operator.EQ, appId);
		filters.add(appidfilter);
		QueryFilter platformidfilter = new QueryFilter("platformid", Operator.EQ, platfrom);
		filters.add(platformidfilter);
		// 添加排序条件
		Map<String, Object> paramsOrder = new HashMap<String, Object>();
		paramsOrder.put("create_time", Direction.DESC);
		// 抽象成mongodb查询语句
		QuerySpecs querySpecs = new QuerySpecs(filters);
		querySpecs.setOrdersByParamsMap(paramsOrder);
		// 组合查询条件
		DynamicQueryBuilder byQuerySpecs = new DynamicQueryBuilder();
		Query query = byQuerySpecs.byQuerySpecs(querySpecs);
		// 执行查询操作
		List<AppVersion> appVersionlsit = new AppVersion().findListByQuery(query);
		List<AppVersionVo> result = new ArrayList<AppVersionVo>();
		if (appVersionlsit != null) {
			AppVersionVo vo;
			for (int i = 0; i < appVersionlsit.size(); i++) {
				vo = new AppVersionVo();
				BeanHelper.copyProperties(vo, appVersionlsit.get(i));
				result.add(vo);
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppVersionVo> getAppStatus(String id, int status) {
		AppVersion appVersion = new AppVersion();
		List<QueryFilter> filters = new ArrayList<QueryFilter>();
		QueryFilter appidfilter = new QueryFilter("applicationid", Operator.EQ, id);
		filters.add(appidfilter);
		QueryFilter versionfilter = new QueryFilter("status", Operator.EQ, status);
		filters.add(versionfilter);
		DynamicQueryBuilder byQuerySpecs = new DynamicQueryBuilder();
		QuerySpecs querySpecs = new QuerySpecs(filters);
		Map<String, Object> paramsOrder = new HashMap<String, Object>();
		paramsOrder.put("platformid", Direction.ASC);
		querySpecs.setOrdersByParamsMap(paramsOrder);
		Query query = byQuerySpecs.byQuerySpecs(querySpecs);
		List<AppVersion> appVersionlsit = appVersion.findListByQuery(query);
		if (appVersionlsit != null) {
			List<AppVersionVo> result = new ArrayList<AppVersionVo>();
			AppVersionVo vo;
			for (int i = 0; i < appVersionlsit.size(); i++) {
				vo = new AppVersionVo();
				BeanHelper.copyProperties(vo, appVersionlsit.get(i));
				result.add(vo);
			}
			return result;
		}
		return null;
	}
	
	/** 获取已上架版本的平台信息 add by heyw
	 * @param appId
	 * @param status
	 * @param platform
	 * @return
	 */
	@Override
	public List<AppVersionVo> getAppStatus_platform(String appId, int status, String platform) {
		AppVersion appVersion = new AppVersion();
		List<QueryFilter> filters = new ArrayList<QueryFilter>();
		QueryFilter appidfilter = new QueryFilter("applicationid", Operator.EQ, appId);
		filters.add(appidfilter);
		QueryFilter versionfilter = new QueryFilter("status", Operator.EQ, status);
		filters.add(versionfilter);
		QueryFilter platformfilter = new QueryFilter("platformid", Operator.EQ, platform);
		filters.add(platformfilter);
		DynamicQueryBuilder byQuerySpecs = new DynamicQueryBuilder();
		QuerySpecs querySpecs = new QuerySpecs(filters);
		Map<String, Object> paramsOrder = new HashMap<String, Object>();
		paramsOrder.put("platformid", Direction.ASC);
		querySpecs.setOrdersByParamsMap(paramsOrder);
		Query query = byQuerySpecs.byQuerySpecs(querySpecs);
		List<AppVersion> appVersionlsit = appVersion.findListByQuery(query);
		if (appVersionlsit != null) {
			List<AppVersionVo> result = new ArrayList<AppVersionVo>();
			AppVersionVo vo;
			for (int i = 0; i < appVersionlsit.size(); i++) {
				vo = new AppVersionVo();
				BeanHelper.copyProperties(vo, appVersionlsit.get(i));
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<AppVersionVo> findListByVersion(String appid, String version) {
		// TODO Auto-generated method stub
		AppVersion appVersion = new AppVersion();
		List<QueryFilter> filters = new ArrayList<QueryFilter>();
		QueryFilter appidfilter = new QueryFilter("applicationid", Operator.EQ, appid);
		filters.add(appidfilter);
		QueryFilter versionfilter = new QueryFilter("version", Operator.EQ, version);
		filters.add(versionfilter);
		DynamicQueryBuilder byQuerySpecs = new DynamicQueryBuilder();
		QuerySpecs querySpecs = new QuerySpecs(filters);
		Map<String, Object> paramsOrder = new HashMap<String, Object>();
		paramsOrder.put("platformid", Direction.ASC);
		querySpecs.setOrdersByParamsMap(paramsOrder);
		Query query = byQuerySpecs.byQuerySpecs(querySpecs);
		List<AppVersion> appVersionlsit = appVersion.findListByQuery(query);
		if (appVersionlsit != null) {
			List<AppVersionVo> result = new ArrayList<AppVersionVo>();
			AppVersionVo vo;
			for (int i = 0; i < appVersionlsit.size(); i++) {
				vo = new AppVersionVo();
				BeanHelper.copyProperties(vo, appVersionlsit.get(i));
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppVersionVo findByVersion(String platfrom, String appid, String version) {
		AppVersion appVersion = new AppVersion();
		List<AppVersion> appVersionlsit = appVersion.findListByProperty(new String[] { "platformid", 
				"applicationid", "version" }, new OperatEnum[] {OperatEnum.EQ, OperatEnum.EQ, OperatEnum.EQ },
				new Object[] { platfrom, appid, version });
		if (appVersionlsit != null && appVersionlsit.size() > 0) {
			AppVersionVo vo = new AppVersionVo();
			BeanHelper.copyProperties(vo, appVersionlsit.get(0));
			return vo;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppVersionVo findBySeq(String seq) {
		AppVersion appVersion = new AppVersion();
		List<AppVersion> appVersionlsit = appVersion.findListByProperty("seq",
				OperatEnum.EQ, seq);
		if (appVersionlsit != null && appVersionlsit.size() > 0) {
			AppVersionVo vo = new AppVersionVo();
			BeanHelper.copyProperties(vo, appVersionlsit.get(0));
			return vo;
		}
		return null;
	}

	@Override
	public void deleteByApp(String appId) {
		AppVersion appVersion = new AppVersion();
		appVersion.deleteByProperty("applicationid", OperatEnum.EQ, appId);
	}

	@Override
	public void addAppSnapshot(String appVId, String snapshotId) {
		AppVersionVo appVersionVo = this.getEntity(appVId);
		if (appVersionVo != null) {
			List<String> snapIdList = appVersionVo.getSnapshotIds();
			snapIdList.add(snapshotId);
			appVersionVo.setSnapshotIds(snapIdList);
			this.saveEntity(appVersionVo);
		}
	}

	@Override
	public void deleteSnapshot(String appVId, String snapshotId) {
		AppVersionVo appVersionVo = this.getEntity(appVId);
		if (appVersionVo != null) {
			List<String> snapIdList = appVersionVo.getSnapshotIds();
			snapIdList.remove(snapshotId);
			appVersionVo.setSnapshotIds(snapIdList);
			this.saveEntity(appVersionVo);
		}
	}

	public int[] checkBuild(String platform, String build, String appId) {
		int[] _result = new int[2];
		_result[0] = 1;
		_result[1] = 0;
		int newBuild = Integer.valueOf(build);
		List<AppVersionVo> appVersions = this.findByPlatform(platform, appId);
		if (null != appVersions && !appVersions.isEmpty()
				&& appVersions.get(0).getBuild() >= newBuild) {
			_result[1] = appVersions.get(0).getBuild();
			_result[0] = 0;

		}
		return _result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AppVersionVo> getAppVersionByPlatformAndVersion(String appId,
			String appVersion, String platform) {
		AppVersion app = new AppVersion();
		 List<AppVersion> appVersionList = app.findListByProperty(new String[]{"applicationid","version","platformid"},
				new OperatEnum[]{OperatEnum.EQ,OperatEnum.EQ,OperatEnum.EQ}, 
				new String[]{appId,appVersion,platform});
		
		if (appVersionList != null && appVersionList.size() > 0) {
				List<AppVersionVo> result = new ArrayList<AppVersionVo>();
				AppVersionVo vo = null;
				for (int i = 0; i < appVersionList.size(); i++) {
					vo = new AppVersionVo();
					BeanHelper.copyProperties(vo, appVersionList.get(i));
					result.add(vo);
				}
				return result;
		}
		
		return null;
	}

	
	public int findBycount(String platform, String appId) {
		// TODO Auto-generated method stub
		List<AppVersionVo> appVersions = this.findByPlatform(platform, appId);
		int count = 0;
		for (AppVersionVo vo : appVersions) {
			count += vo.getVersion_counter();
		}
		return count;
	}
}
